#include "ControlBarWidget.h"
#include <SEUi/SEUi.h>
#include <SEEdit/Layer/EditLayer.h>
#include <SECore/CoreApplication.h>
#include <SECore/Timer.h>
#include <SEDebug/Log/Log.h>
#include <SEEdit/Descriptors/ControlBarStateDescriptor.h>
#include <SEngine/Ecs/Components/ScriptComp.h>
#include <SEngine/Ecs/Components/RigidBodyComp.h>

#include <SEngine/Project/Project.h>

namespace SEngine
{

    ControlBarWidget::ControlBarWidget(EditLayer & layer)
        : Widget()
        , m_layer(layer)
    {
        CoreApplication::App().AddDescriptor<ControlBarStateDescriptor>();
        CoreApplication::App().GetLayer("PhysicsLayer")->get().SetEnabled(false);
        CoreApplication::App().GetLayer("AnimationLayer")->get().SetEnabled(false);
    }

    void ControlBarWidget::UpdateImpl(int delta)
    {
        auto & state = CoreApplication::App().GetDescriptor<ControlBarStateDescriptor>()->get();
        if (ImGui::Button(state.isPlaying ? "Stop" : "Play")) { 
            state.isPlaying = !state.isPlaying;
            CoreApplication::App().GetDescriptor<Project>()->get().GetScene()->Components<ScriptComp>([&](Actor & actor, ScriptComp& comp) { comp.m_enabled = state.isPlaying; });
            CoreApplication::App().GetDescriptor<Project>()->get().GetScene()->Components<RigidBodyComp>([&](Actor & actor, RigidBodyComp& comp) { state.isPlaying ? comp.StartSimulation() : comp.StopSimulation(); });
            CoreApplication::App().GetLayer("PhysicsLayer")->get().SetEnabled(state.isPlaying);
            CoreApplication::App().GetLayer("AnimationLayer")->get().SetEnabled(state.isPlaying);
        }
    }

} // namespace SEngine
